package com.jiangjiang.eshop.membership.updater;

import com.jiangjiang.eshop.membership.constant.MemberLevel;
import com.jiangjiang.eshop.membership.domain.MemberLevelDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberLevelEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointDetailEntity;
import com.jiangjiang.eshop.membership.domain.MemberPointEntity;
import com.jiangjiang.eshop.membership.service.MemberLevelDetailService;
import com.jiangjiang.eshop.membership.service.MemberLevelService;
import com.jiangjiang.eshop.membership.service.MemberPointDetailService;
import com.jiangjiang.eshop.membership.service.MemberPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName FIrstLoginUpdater
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/6 21:40
 * @Version 1.0
 **/
@Component
public class FirstLoginMembershipUpdater extends AbstractMembershipUpdater<Object,Boolean> {

    /**
     * 每天第一次登录对成长值的累加值
     */
    private static final Long FIRST_LOGIN_GROWTH_VALUE_UPDATE = 5L;
    /**
     * 每天第一次登录对会员积分的累加值
     */
    private static final Long FIRST_LOGIN_MEMBER_POINT_UPDATE = 5L;

    /**
     * 会员等级管理DAO组件
     */
    @Autowired
    private MemberLevelService memberLevelService;
    /**
     * 会员积分管理DAO组件
     */
    @Autowired
    private MemberPointService memberPointService;

    @Autowired
    public FirstLoginMembershipUpdater(
            MemberLevelDetailService memberLevelDetailService,
            MemberPointDetailService memberPointDetailService) {
        super(memberLevelDetailService, memberPointDetailService);
    }

    @Override
    protected MemberLevelDetailEntity updateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        MemberLevelDetailEntity detail = new MemberLevelDetailEntity();
        MemberLevelEntity entity = memberLevelService.queryByAccountId(userAccountId);
        // 记录更新之前的旧值
        detail.setOldGrowthValue(entity.getGrowthValue());
        detail.setOldMemberLevel(entity.getLevel());

        // 开始数据更新
        entity.setGrowthValue(entity.getGrowthValue() + FIRST_LOGIN_GROWTH_VALUE_UPDATE);
        entity.setLevel(MemberLevel.get(entity.getGrowthValue()));
        memberLevelService.updateById(entity);
        // 记录更新之后的值
        detail.setNewGrowthValue(entity.getGrowthValue());
        detail.setNewMemberLevel(entity.getLevel());
        detail.setUpdatedGrowthValue(FIRST_LOGIN_GROWTH_VALUE_UPDATE);
        return detail;
    }

    @Override
    protected MemberPointDetailEntity updateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        MemberPointDetailEntity detail = new MemberPointDetailEntity();
        MemberPointEntity entity = memberPointService.queryByAccountId(userAccountId);
        // 记录更新之前的旧值
        detail.setOldMemberPoint(entity.getPoint());

        // 开始数据更新
        entity.setPoint(entity.getPoint() + FIRST_LOGIN_MEMBER_POINT_UPDATE);
        memberPointService.updateById(entity);
        // 记录更新之后的值
        detail.setNewMemberPoint(entity.getPoint());
        detail.setUpdatedMemberPoint(FIRST_LOGIN_MEMBER_POINT_UPDATE);
        return detail;
    }

    @Override
    protected String getMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return "用户每天第一次登录";
    }

    @Override
    protected String getMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return "用户每天第一次登录";
    }


    @Override
    protected Boolean getExecuteResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }

    @Override
    protected MemberLevelDetailEntity undoUpdateMemberLevel(Long userAccountId, Object parameter) throws Exception {
        // 第一次登录不能撤销
        return null;
    }

    @Override
    protected MemberPointDetailEntity undoUpdateMemberPoint(Long userAccountId, Object parameter) throws Exception {
        // 第一次登录不能撤销
        return null;
    }

    @Override
    protected String getUndoMemberLevelUpdateReason(Long userAccountId, Object parameter) {
        return null;
    }

    @Override
    protected String getUndoMemberPointUpdateReason(Long userAccountId, Object parameter) {
        return null;
    }


    @Override
    protected Boolean getUndoResult(Long userAccountId, Object parameter) throws Exception {
        return true;
    }
}
